package xm.util
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.utils.Dictionary;
	
	import xm.manager.StageManager;
	
	/**
	 * ( ex )
	 * <br />
	 * FileSyncUtil.getInstance().loadFile( file, onLoadHanler );
	 * <br />
	 * <br />
	 * //인자로 반드시 file 을 받아야 한다.
	 * <br />
	 * function onLoadHandler( file : File ):void
	 * <br />
	 * {
	 * <br />
	 * 		trace( file.data ); // byteArray
	 * <br />
	 * } 
	 * <br />
	 * <br />
	 * <br />
	 * <br />
	 * <br> 클래스 설명 : </br>
	 * @author 김정환
	 */	
	public class FileSyncUtil
	{
		
		//-----------------------------------------------------------------------------------
		// singleton 
		//-----------------------------------------------------------------------------------
		
		private static var _instance : FileSyncUtil;
		
		
		/**
		 * @return FileSyncUtil
		 */		
		public static function getInstance():FileSyncUtil
		{
			if( _instance == null ) _instance = new FileSyncUtil();
			
			return _instance; 
		}
		
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		/**
		 * fileList
		 */		
		protected var fileList : Array = [];
		
		/**
		 * dicLoadHandler
		 */		
		protected var dicLoadHandler : Dictionary = new Dictionary( true );
		
		/**
		 * dicOpenCheck
		 */		
		protected var dicOpenCheck : Dictionary = new Dictionary( true );
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * <br />
		 * ( ex )
		 * <br />
		 * FileSyncUtil.getInstance().loadFile( file, onLoadHanler );
		 * <br />
		 * <br />
		 * 인자로 반드시 file 을 받아야 한다.
		 * <br />
		 * function onLoadHandler( file : File ):void
		 * <br />
		 * {
		 * <br />
		 * 		trace( file.data ); // byteArray
		 * <br />
		 * } 
		 * <br />
		 * <br />
		 * <br />
		 * <br />
		 * constructor 
		 */	
		public function FileSyncUtil()
		{
			
		}
		
		/**
		 * ( ex )
		 * <br />
		 * FileSyncUtil.getInstance().loadFile( file, onLoadHanler );
		 * <br />
		 * <br />
		 * //인자로 반드시 file 을 받아야 한다.
		 * <br />
		 * function onLoadHandler( file : File ):void
		 * <br />
		 * {
		 * <br />
		 * 		trace( file.data ); // byteArray
		 * <br />
		 * } 
		 * <br />
		 * <br />
		 * @param file
		 * @param onLoadHandler - file : File 인자가 있어야 함.
		 * 
		 */		
		public function loadFile( file : File, onLoadHandler : Function ):void
		{
			var s : Stage = StageManager.getInstance().stage;
			
			file.addEventListener(Event.OPEN, onFileOpen );
			file.addEventListener(Event.COMPLETE, onFileLoadComplete );
			
			fileList.push( file );
			dicLoadHandler[ file ] = onLoadHandler;
			dicOpenCheck[ file ] = false;
			
			s.addEventListener(Event.ENTER_FRAME, onSyncFileChecker );
		}
		
		
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		
		
		/**
		 * 
		 * @param e
		 * 
		 */		
		protected function onFileLoadComplete(e:Event):void
		{
			var file : File = e.currentTarget as File;
			
			dicLoadHandler[ file ]( file );
			
			file.removeEventListener(Event.OPEN, onFileOpen );
			file.removeEventListener(Event.COMPLETE, onFileLoadComplete );
			delete dicLoadHandler[ file ];
			delete dicOpenCheck[ file ];
			
			fileList.splice( fileList.indexOf( file ), 1 );
		}
		
		
		/**
		 * 
		 * @param e
		 * 
		 */		
		protected function onFileOpen(e:Event):void
		{
			var file : File = e.currentTarget as File;
			dicOpenCheck[ file ] = true;
		}
		
		
		
		/**
		 * 
		 * @param e
		 * 
		 */		
		protected function onSyncFileChecker(e:Event):void
		{
			if( fileList.length == 0 )
			{
				e.currentTarget.removeEventListener( e.type, arguments.callee, e.bubbles );
				return; 
			}
			
			
			var i:int, len:int, file:File;
			
			i=0;
			len=fileList.length;
			
			while( i<len )
			{
				file = fileList[ i ] as File;
				if( dicOpenCheck[ file ] == false ) file.load();
				++i;
			}
			
		}		
		
		
		
		
		
		
	}
}